using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace EditorExtensions
{

    public class UndoExample : MonoBehaviour
    {
        [MenuItem("EditorExtensions/07.Undo/CreateObj")]
        static void CreateObj()
        {
            var newObj = new GameObject("Undo");
            Undo.RegisterCreatedObjectUndo(newObj, "CreateObj");
        }
        [MenuItem("EditorExtensions/07.Undo/MoveObj")]
        static void Moveobj()
        {
            var trans = Selection.activeGameObject.transform;
            if (trans)
            {
                Undo.RecordObject(trans, "MoveObj");
                trans.position += Vector3.up * 10f;
            }
        }
        
        [MenuItem("EditorExtensions/07.Undo/AddComponentObj")]
        static void AddComponentObj()
        {
            var selectObj = Selection.activeGameObject;
            if (selectObj)
            {
                Undo.AddComponent(selectObj, typeof(Rigidbody));
            }
        }
        
        [MenuItem("EditorExtensions/07.Undo/DestroyObj")]
        static void DestroyObj()
        {
            var selectObj = Selection.activeGameObject;
            if (selectObj)
            {
                Undo.DestroyObjectImmediate(selectObj);
            }
        }
        
        [MenuItem("EditorExtensions/07.Undo/SetParentObj")]
        static void SetParentObj()
        {
            var transform = Selection.activeGameObject.transform;
            var root = Camera.main.transform;
            if (transform)
            {
                Undo.SetTransformParent(transform, root, transform.name);
            }
        }

        [MenuItem("EditorExtensions/07.Undo/AddComponentObj", validate = true)]
        [MenuItem("EditorExtensions/07.Undo/DestroyObj", validate = true)]
        [MenuItem("EditorExtensions/07.Undo/SetParentObj", validate = true)]
        [MenuItem("EditorExtensions/07.Undo/MoveObj", validate = true)]
        static bool Check()
        {
            return Selection.activeGameObject != null;
        }

    }
}

